import { Mut,Computed, VueService } from 'vue3-oop'
import { Injectable } from "injection-js";
import type { ISettings } from '@/types/settings';
import { defaultSetting } from '@/types/settings';

type ISettingsKey = keyof ISettings;

const localSettingsKey = 'localSettings';
const localSettings: ISettings = JSON.parse(window.localStorage.getItem(localSettingsKey) || '{}')

@Injectable()
export default class SettingSercice extends VueService {
  static instCount = 0;

  @Mut() settings:ISettings = Object.assign({},defaultSetting,localSettings);

  constructor() {
    super();
    SettingSercice.instCount++>1 && console.error("SettingSercice.instCount="+SettingSercice.instCount)
  }

  @Computed()
  get inverted(){
    return this.settings.globalTheme === 'sideDarkTheme';
  }

  changeSetting=(key: ISettingsKey, data: string | boolean)=>{
    this.settings[key] = data as never;
  }

  saveSetting=()=>{
    window.localStorage.setItem(localSettingsKey, JSON.stringify(this.settings));
    window.$message.success('保存配置成功！')
    window.location.reload()
  }

  resetSetting = () => {
    window.localStorage.removeItem(localSettingsKey)
    window.$message.success('重置配置成功！')
    window.location.reload()
  }
}
